package com.ku.rocketmq.quickstart.pullconsumer;

import com.ku.rocketmq.constants.Const;
import org.apache.rocketmq.client.exception.MQBrokerException;
import org.apache.rocketmq.client.exception.MQClientException;
import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.common.message.Message;
import org.apache.rocketmq.remoting.exception.RemotingException;

import java.util.concurrent.TimeUnit;

/**
 * @program: rocketmqDemo
 * @description: Pull方式主动拉取消息
 * @author: JokerKu
 * @create: 2020-05-04 12:45
 **/
public class Provider {

    public static void main(String[] args) throws MQClientException, RemotingException, InterruptedException, MQBrokerException {
        //1.创建默认的mq 并指定组名
        DefaultMQProducer defaultMQProducer = new DefaultMQProducer("test_quick_producer_name");
        //2.设置mq nameserver 地址
        defaultMQProducer.setNamesrvAddr(Const.NAMESERVER_ADDR);
        //3.启动生产者
        defaultMQProducer.start();

        //4.发送消息
        for (int i = 0; i < 3; i++) {
            //4.1创建消息
            Message message = new Message(
                    "test_pull_topic",//topic:主题
                    "TagA",//标签
                    "keyA" + i,//用户自定义的key 唯一标识
                    ("Hello RocketMQ" + i).getBytes()//要发送的消息内容实体(byte[])
            );
            SendResult send = defaultMQProducer.send(message);
            System.out.println("发送结果:"+send.getSendStatus());
            TimeUnit.SECONDS.sleep(1); //间隔1s
        }

        defaultMQProducer.shutdown();
    }
}
